package com.fhcodeintegration.demo07.controller;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.WriterException;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;

@RestController
@RequestMapping("/code")
public class CodeController {
    @GetMapping("/qrcode")
    public void qrcodeTest(HttpServletResponse response) throws WriterException, IOException {
        ServletOutputStream outputStream = response.getOutputStream();
        int width = 300;
        int height =300;
        String format = "png";
        HashMap<EncodeHintType, Object> map = new HashMap<>();
        map.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
        map.put(EncodeHintType.CHARACTER_SET,"utf-8");
        String content = "今年是兔年，兔飞猛进！猪兔猛进！";
        BitMatrix encode = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, width, height, map);
        MatrixToImageWriter.writeToStream(encode,format,outputStream);
        System.out.println("成功！");
    }
}
